using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;

namespace Portal.GUI.EditoralOffice.MainOffce.NewsEditModules.Video
{
    public partial class VideoEdit : Portal.BO.Editoral.NewsEditModules.NewsModule
    {
        private string uploadFileLocation = "/GUI/EditoralOffice/MainOffce/NewsEditModules/Video/Upload/";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bindData();
            }
        }

        private void bindData()
        {  
            LoadRuntimeSettings();

            try
            {
                drpVideoType.SelectedValue = Convert.ToString(ReadRuntimeConfig(true, "VideoType"));
            }
            catch { }
            txtFile.Text = Convert.ToString(ReadRuntimeConfig(true, "VideoUrl"));
            txtWidth.Text = Convert.ToString(ReadRuntimeConfig(true, "Width"));
            txtHeight.Text = Convert.ToString(ReadRuntimeConfig(true, "Height"));
            chkAutoStart.Checked = (Convert.ToString(ReadRuntimeConfig(true, "AutoStart")) == "1" ? true : false);
            txtPlayCount.Text = Convert.ToString(ReadRuntimeConfig(true, "PlayCount"));
        }

        protected void btnOk_Click(object sender, EventArgs e)
        {
            save();
        }

        public override void SaveAllProperties()
        {
            save();
        }

        private void save()
        {
            if (txtFile.Text.Trim() == "") return;

            PortalDefinition.Module module = new PortalDefinition.Module();
            module.reference = this.ModuleRef;
            module.type = this.ModuleType;
            module.LoadRuntimeProperties();

            module.moduleRuntimeSettings.SetRuntimePropertyValue(true, "VideoType", drpVideoType.SelectedValue);
            module.moduleRuntimeSettings.SetRuntimePropertyValue(true, "VideoUrl", txtFile.Text);
            module.moduleRuntimeSettings.SetRuntimePropertyValue(true, "Width", txtWidth.Text.Trim());
            module.moduleRuntimeSettings.SetRuntimePropertyValue(true, "Height", txtHeight.Text.Trim());
            module.moduleRuntimeSettings.SetRuntimePropertyValue(true, "AutoStart", chkAutoStart.Checked ? "1" : "0");
            module.moduleRuntimeSettings.SetRuntimePropertyValue(true, "PlayCount", txtPlayCount.Text.Trim());

            module.SaveRuntimeSettings();
        }

        protected void btnUpload_Click(object sender, EventArgs e)
        {
            if (!fuVideoFile.HasFile) return;

            string uploadPath = uploadFileLocation + DateTime.Now.ToString("yyyyMMddhhmmss") + "/";
            string uploadDir = Server.MapPath(uploadPath);
            if (!Directory.Exists(uploadDir))
                Directory.CreateDirectory(uploadDir);
            fuVideoFile.SaveAs(uploadDir + fuVideoFile.FileName);
            //flVideoFile.PostedFile.SaveAs(uploadDir + flVideoFile.PostedFile.FileName);
            txtFile.Text = uploadPath + fuVideoFile.FileName;
        }
    }
}